package tech.youai.ppping;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import tech.youai.nav.SimpleUrlBuilderFactory;
import tech.youai.nav.UrlBuilderFactory;

import java.util.HashMap;
import java.util.Map;

@org.springframework.context.annotation.Configuration

public class Configuration {
    @Value("${pp.ping.urls}")
    private String urls;
    @Value("${wx.cp.corpId}")
    private String corpId;
    @Value("${wx.cp.corpSecret}")
    private String corpSecret;
    @Value("${wx.cp.agentId:0}")
    private Integer agentId;

    public Map<String, String> getUrlsMap(){
        Map<String, String> urlsMap = new HashMap<>();
        String[] keyValues = getUrls().split("\\|");
        for (String s: keyValues){
            String[] keyVal = s.split("::");
            if(keyVal.length==2){
                urlsMap.put(keyVal[0], keyVal[1]);
            }
        }
        return urlsMap;
    }
    @Bean
    AlertManager alertManager(Weixin weixin){
        return new AlertManager(getUrlsMap(), weixin);
    }
    @Bean
    Weixin weixin(UrlBuilderFactory urlBuilderFactory){
        return new Weixin(this, urlBuilderFactory);
    }
    @Bean
    UrlBuilderFactory urlBuilderFactory(){
        return new SimpleUrlBuilderFactory();
    }

    public String getCorpId() {
        return corpId;
    }

    public void setCorpId(String corpId) {
        this.corpId = corpId;
    }

    public String getCorpSecret() {
        return corpSecret;
    }

    public void setCorpSecret(String corpSecret) {
        this.corpSecret = corpSecret;
    }

    public Integer getAgentId() {
        return agentId;
    }

    public void setAgentId(Integer agentId) {
        this.agentId = agentId;
    }

    public String getUrls() {
        return urls;
    }

    public void setUrls(String urls) {
        this.urls = urls;
    }
}
